package com.example.cushion.realize.setting;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.example.cushion.R;
import com.example.cushion.adapter.MenuItemAdapter;
import com.example.cushion.base.BaseCommonActivity;
import com.example.cushion.bean.MyMenuItem;
import com.example.cushion.binddata.DataSettingLanguage;
import com.example.cushion.global.SpKey;
import com.example.cushion.ui.DividerManager;
import com.example.cushion.ui.MenuUtils;

import org.itzheng.and.baseutils.common.LanguageUtils;
import org.itzheng.and.baseutils.common.SPUtils;
import org.itzheng.and.baseutils.log.LogHelper;
import org.itzheng.and.baseutils.ui.UIUtils;
import org.itzheng.and.recyclerview.adapter.BaseRecyclerAdapter;

import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Title:<br>
 * Description: <br>
 *
 * @email ItZheng@ZoHo.com
 * Created by itzheng on 2018-3-8.
 */
public class SettingLanguageActivity extends BaseCommonActivity {
    @BindView(R.id.rcvContent)
    RecyclerView rcvContent;
    private static final String TAG = "SettingLanguageActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogHelper.d(TAG, "onCreate");
        setTitle();
        setContentView(R.layout.container_recycler_view);
        ButterKnife.bind(this);
        setAdapter();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(UIUtils.getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rcvContent.setLayoutManager(linearLayoutManager);
        rcvContent.addItemDecoration(DividerManager.getCommonLinearLayoutDecoration());
        LogHelper.d(TAG, "onCreate end");
    }

    private void setTitle() {
        setTitle(getString(R.string.setting_language_title));
    }

    private void setAdapter() {
        final List<MyMenuItem> languages = DataSettingLanguage.getLanguages(getActivity());
        MenuItemAdapter adapter = new MenuItemAdapter(languages);
        adapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                MyMenuItem menuItem = languages.get(position);
                showLoading();
                getWindow().getDecorView().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        dismissLoading();
                    }
                }, 2000);
                if (menuItem.tag instanceof Locale) {
                    Locale locale = (Locale) menuItem.tag;
                    //AppContent 和Activity 获取的资源文件不一样，所以，两个地方都需要设置语言
                    //App主要是对于content.getText Activity 对于xml布局
                    LanguageUtils.setAppLanguage(UIUtils.getContext(), locale);
                    LanguageUtils.setAppLanguage(getActivity(), locale);
                    if (position == 0) {
                        SPUtils.putString(SpKey.STRING_APP_LANGUAGE, "");
                    } else {
                        SPUtils.putString(SpKey.STRING_APP_LANGUAGE, locale.toString());
                    }
                }
                setAdapter();
                setTitle();
//                EventBus.getDefault().post(new EventBusLogic(EventBusLogic.EVENT_WHAT_REFRESH_LANGUAGE));
            }
        });
        rcvContent.setAdapter(adapter);
//
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        LogHelper.d(TAG, "onCreateOptionsMenu");
        final MenuItem menuAdd = MenuUtils.createMenuAdd(menu);
        menuAdd.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                View view = menuAdd.getActionView();
                UIUtils.showToast("(view == null)" + (view == null));
                return false;
            }
        });

        return super.onCreateOptionsMenu(menu);
    }
}
